//
//  FKMemberCenterVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/13.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMemberCenterVC.h"
#import "FKTeamAwardVC.h"
#import "FKIncomeDetailVC.h"
#import "FKCustomBtn.h"
#import "FKMyProfitVC.h"
#import "FKTeamMemberVC.h"
#import "FKMemberChildVC.h"

@interface FKMemberCenterVC ()

@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *levelBtn;
@property (weak, nonatomic) IBOutlet UIButton *inviteCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *joinDaysLabel;

@property (weak, nonatomic) IBOutlet UILabel *totalProfitLabel;
@property (weak, nonatomic) IBOutlet UILabel *todayProfitLabel;
@property (weak, nonatomic) IBOutlet UILabel *monthProfitLabel;

/** 团队奖励 */
@property (weak, nonatomic) IBOutlet UILabel *teamPrizeLabel;
/** 可提现余额 */
@property (weak, nonatomic) IBOutlet UILabel *canWithdrawLabel;
/** 佣金提成 */
@property (weak, nonatomic) IBOutlet UILabel *commissionLabel;
/** 培训津贴 */
@property (weak, nonatomic) IBOutlet UILabel *trainProfitLabel;
/** 去提现 */
@property (weak, nonatomic) IBOutlet FKCustomBtn *goWithdrawBtn;

@property (weak, nonatomic) IBOutlet UILabel *inviteNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *fansNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *teamNumLabel;
@property (weak, nonatomic) IBOutlet UIImageView *adView;

@property (nonatomic, copy) NSString *invite_code;

@end

@implementation FKMemberCenterVC

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKMemberCenterVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.teamPrizeLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(teamPrizeDidTap:)]];
    [self.commissionLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commissionLabelDidTap:)]];
    [self.trainProfitLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(trainProfitLabelDidTap:)]];
    [self.inviteNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(inviteNumLabelDidTap:)]];
    [self.fansNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fansNumLabelDidTap:)]];
    [self.teamNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(teamNumLabelDidTap:)]];
    
    [self.avatarView fk_viewCornerRadius:28 borderWidth:0 borderColor:nil];
    self.goWithdrawBtn.imagePosition = FKImagePositionRight;
    [self.goWithdrawBtn fk_viewCornerRadius:12 borderWidth:0.8 borderColor:[UIColor fk_colorWithHex:0xaeaeae]];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self loadData];
//    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - action
- (IBAction)copyCodeBtnClicked:(UIButton *)sender
{
    if (self.invite_code) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.invite_code;
        [SVProgressHUD showSuccessWithStatus:@"邀请码已复制到剪贴板"];
    }
}
- (IBAction)withdrawBtnClicked:(UIButton *)sender
{
    FKMyProfitVC *vc = [[FKMyProfitVC alloc] init];
    fkPushNav(vc);
}

- (void)teamPrizeDidTap:(UITapGestureRecognizer *)gesture
{
    FKTeamAwardVC *vc = [[FKTeamAwardVC alloc] init];
    vc.type = @"30";
    fkPushNav(vc);
}

- (void)commissionLabelDidTap:(UITapGestureRecognizer *)gesture
{
    FKIncomeDetailVC *vc = [[FKIncomeDetailVC alloc] init];
    fkPushNav(vc);
}

- (void)trainProfitLabelDidTap:(UITapGestureRecognizer *)gesture
{
    FKTeamAwardVC *vc = [[FKTeamAwardVC alloc] init];
    vc.type = @"40";
    fkPushNav(vc);
}

- (void)inviteNumLabelDidTap:(UITapGestureRecognizer *)gesture
{
    FKMemberChildVC *vc = [[FKMemberChildVC alloc] init];
    vc.title = @"已邀用户";
    vc.type = @"10";
    [vc refreshData];
    fkPushNav(vc);
}

- (void)fansNumLabelDidTap:(UITapGestureRecognizer *)gesture
{
    FKMemberChildVC *vc = [[FKMemberChildVC alloc] init];
    vc.title = @"浏览粉丝";
    vc.type = @"20";
    [vc refreshData];
    fkPushNav(vc);
}

- (void)teamNumLabelDidTap:(UITapGestureRecognizer *)gesture
{
    FKTeamMemberVC *vc = [[FKTeamMemberVC alloc] init];
    vc.title = @"团队成员";
//    vc.type = @"40";
    fkPushNav(vc);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=index" param:nil success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            NSDictionary *memberInfo = result.data[@"userInfo"];
            NSDictionary *ad = result.data[@"ad"];
            [self.adView sd_setImageWithURL:[NSURL URLWithString:ad[@"content"]] placeholderImage:nil];
            //  设置数据
            [self.avatarView sd_setImageWithURL:[NSURL URLWithString:memberInfo[@"avatar"]] placeholderImage:kDefaultAvatar];
            self.nameLabel.text = memberInfo[@"nickname"];
            NSString *levelName = fkFORMAT(@"member_%@", memberInfo[@"level"]);
            NSString *level = memberInfo[@"level"];
            NSString *levelTitle = @"VIP";
            if ([level isEqualToString:@"30"]) {
                levelTitle = @"总监";
            } else if ([level isEqualToString:@"40"]) {
                levelTitle = @"总经理";
            }
            [self.levelBtn setTitle:levelTitle forState:UIControlStateNormal];
            [self.levelBtn setImage:[UIImage imageNamed:levelName] forState:UIControlStateNormal];
            self.joinDaysLabel.text = fkFORMAT(@"%@\n加入天数", memberInfo[@"becomeLevelTotalDay"]);
            self.invite_code = memberInfo[@"invite_code"];
            [self.inviteCodeBtn setTitle:fkFORMAT(@"邀请码: %@", memberInfo[@"invite_code"]) forState:UIControlStateNormal];
            
            self.totalProfitLabel.text = fkFORMAT(@"累计总收益(元)\n%@", memberInfo[@"total_porfit"]);
            self.totalProfitLabel.attributedText = [self.totalProfitLabel.text fk_addAttributeStyle:@"累计总收益(元)" color:[UIColor fk_colorWithHex:0xa7793b] font:fk_adjustFont(12)];
            
            self.todayProfitLabel.text = fkFORMAT(@"今日收益(元)\n%@", memberInfo[@"todayPorfit"]);
            self.todayProfitLabel.attributedText = [self.todayProfitLabel.text fk_addAttributeStyle:@"今日收益(元)" color:[UIColor fk_colorWithHex:0xa7793b] font:fk_adjustFont(12)];
            
            self.monthProfitLabel.text = fkFORMAT(@"近30天收益(元)\n%@", memberInfo[@"monthPorfit"]);
            self.monthProfitLabel.attributedText = [self.monthProfitLabel.text fk_addAttributeStyle:@"近30天收益(元)" color:[UIColor fk_colorWithHex:0xa7793b] font:fk_adjustFont(12)];
            
            self.teamPrizeLabel.text = fkFORMAT(@"%@\n团队奖励(元)", memberInfo[@"train_reward"]);
            self.teamPrizeLabel.attributedText = [self.teamPrizeLabel.text fk_addAttributeStyle:@"团队奖励(元)" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.canWithdrawLabel.text = fkFORMAT(@"%@\n可提现余额(元)", memberInfo[@"withdrawPorfit"]);
            self.canWithdrawLabel.attributedText = [self.canWithdrawLabel.text fk_addAttributeStyle:@"可提现余额(元)" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.commissionLabel.text = fkFORMAT(@"%@\n佣金提成(元)", memberInfo[@"commission"]);
            self.commissionLabel.attributedText = [self.commissionLabel.text fk_addAttributeStyle:@"佣金提成(元)" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.trainProfitLabel.text = fkFORMAT(@"%@\n培训津贴(元)", memberInfo[@"management_reward"]);
            self.trainProfitLabel.attributedText = [self.trainProfitLabel.text fk_addAttributeStyle:@"培训津贴(元)" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.inviteNumLabel.text = fkFORMAT(@"%@\n已邀用户", memberInfo[@"inviteMemberNum"]);
            self.inviteNumLabel.attributedText = [self.inviteNumLabel.text fk_addAttributeStyle:@"已邀用户" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.fansNumLabel.text = fkFORMAT(@"%@\n浏览粉丝", memberInfo[@"lookMemberNum"]);
            self.fansNumLabel.attributedText = [self.fansNumLabel.text fk_addAttributeStyle:@"浏览粉丝" color:fkColor999999 font:fk_adjustFont(13)];
            
            self.teamNumLabel.text = fkFORMAT(@"%@\n团队成员", memberInfo[@"teamMemberNum"]);
            self.teamNumLabel.attributedText = [self.teamNumLabel.text fk_addAttributeStyle:@"团队成员" color:fkColor999999 font:fk_adjustFont(13)];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
